/*
 * @(#) JobItemServiceImpl.java Sep 10, 2011
 *
 * Copyright (c) 2011, Strong Technology. All Rights Reserved.
 * Strong Technology. CONFIDENTIAL
 */
package cn.strong.gweva.jobeva.business.service.impl;

import java.util.Date;

import cn.strong.gweva.common.business.entity.PagerEntity;
import cn.strong.gweva.common.business.service.impl.AbstractServiceImpl;
import cn.strong.gweva.common.util.Constants;
import cn.strong.gweva.common.util.Constants4Method;
import cn.strong.gweva.jobeva.business.entity.JobItemEntity;
import cn.strong.gweva.jobeva.business.entity.JobItemExEntity;
import cn.strong.gweva.jobeva.business.entity.JobItemSearchEntity;
import cn.strong.gweva.jobeva.business.service.JobItemService;


/**
 * 岗位考核项目Service实现类.
 * 
 * @author jincong
 * @version 1.0
 * @since Sep 10, 2011
 */
public class JobItemServiceImpl extends AbstractServiceImpl implements JobItemService
{

	/**
	 * 岗位考核项目分页查询.
	 * 
	 * @param pagerEntity
	 * @param jobItemSearchEntity
	 * @return 岗位考核项目分页信息
	 * @throws Exception 异常
	 */
	public PagerEntity searchJobItemPage(PagerEntity pagerEntity,
			JobItemSearchEntity jobItemSearchEntity) throws Exception
	{
		return this.getCommonDao().searchPager(pagerEntity,
				Constants4Method.JOBEVA_SEARCH_JOB_ITEM_LIST, jobItemSearchEntity);
	}

	/**
	 * 新增岗位考核项目.
	 * 
	 * @param jobItemEntity 岗位考核项目管理entity
	 * @throws Exception 异常
	 */
	public void insertJobItem(JobItemEntity jobItemEntity) throws Exception
	{
		// 岗位考核项目
		jobItemEntity.getJobItem().setDelFlag(Constants.DEL_FLAG_NO);
		jobItemEntity.getJobItem().setUpdateTime(new Date());
		Integer itemId = (Integer)this.getCommonDao().insert(
				Constants4Method.JOBEVA_INSERT_JOB_ITEM, jobItemEntity.getJobItem());

		if (jobItemEntity.getJobRuleList() != null && jobItemEntity.getJobRuleList().size() > 0)
		{
			for (int i = jobItemEntity.getJobRuleList().size() - 1; i >= 0; i--)
			{
				if (jobItemEntity.getJobRuleList().get(i) == null)
				{
					jobItemEntity.getJobRuleList().remove(i);
					continue;
				}

				jobItemEntity.getJobRuleList().get(i).setItemId(itemId);
				jobItemEntity.getJobRuleList().get(i).setDelFlag(Constants.DEL_FLAG_NO);
				jobItemEntity.getJobRuleList().get(i).setUpdateTime(new Date());
				jobItemEntity.getJobRuleList().get(i).setUpdateUserId(
						jobItemEntity.getJobItem().getUpdateUserId());
			}

			this.getCommonDao().insertBatch(Constants4Method.JOBEVA_INSERT_JOB_RULE,
					jobItemEntity.getJobRuleList());
		}
	}

	/**
	 * 修改岗位考核项目.
	 * 
	 * @param jobItemEntity 岗位考核项目管理entity
	 * @throws Exception 异常
	 */
	public void updateJobItem(JobItemEntity jobItemEntity) throws Exception
	{
		// 更新岗位考核项目
		jobItemEntity.getJobItem().setDelFlag(Constants.DEL_FLAG_NO);
		jobItemEntity.getJobItem().setUpdateTime(new Date());
		this.getCommonDao().update(Constants4Method.JOBEVA_UPDATE_JOB_ITEM,
				jobItemEntity.getJobItem());

		// 逻辑删除岗位考核规则
		jobItemEntity.getJobItem().setDelFlag(Constants.DEL_FLAG_YES);
		jobItemEntity.getJobItem().setUpdateTime(new Date());
		this.getCommonDao().update(
				Constants4Method.JOBEVA_LOGIC_DELETE_JOB_RULE_BY_JOB_ITEM,
				jobItemEntity.getJobItem());

		// 新增
		if (jobItemEntity.getJobRuleList() != null && jobItemEntity.getJobRuleList().size() > 0)
		{
			for (int i = jobItemEntity.getJobRuleList().size() - 1; i >= 0; i--)
			{
				if (jobItemEntity.getJobRuleList().get(i) == null)
				{
					jobItemEntity.getJobRuleList().remove(i);
					continue;
				}

				jobItemEntity.getJobRuleList().get(i).setItemId(
						jobItemEntity.getJobItem().getItemId());
				jobItemEntity.getJobRuleList().get(i).setDelFlag(Constants.DEL_FLAG_NO);
				jobItemEntity.getJobRuleList().get(i).setUpdateTime(new Date());
				jobItemEntity.getJobRuleList().get(i).setUpdateUserId(
						jobItemEntity.getJobItem().getUpdateUserId());
			}

			this.getCommonDao().insertBatch(Constants4Method.JOBEVA_INSERT_JOB_RULE,
					jobItemEntity.getJobRuleList());
		}
	}

	/**
	 * 删除岗位考核项目.
	 * 
	 * @param jobItemEntity 岗位考核项目管理entity
	 * @throws Exception 异常
	 */
	public void deleteJobItem(JobItemEntity jobItemEntity) throws Exception
	{
//		// 逻辑删除岗位考核项目
//		jobItemEntity.getJobItem().setDelFlag(Constants.DEL_FLAG_YES);
//		jobItemEntity.getJobItem().setUpdateTime(new Date());
//		this.getCommonDao().update(Constants4Method.JOBEVA_LOGIC_DELETE_JOB_ITEM,
//				jobItemEntity.getJobItem());
//
//		// 逻辑删除岗位考核规则
//		this.getCommonDao().update(
//				Constants4Method.JOBEVA_LOGIC_DELETE_JOB_RULE_BY_JOB_ITEM,
//				jobItemEntity.getJobItem());
		this.getCommonDao().delete(Constants4Method.JOBEVA_DELETE_JOB_ITEM, jobItemEntity.getJobItem());
		this.getCommonDao().delete(Constants4Method.JOBEVA_DELETE_JOB_RULE_BY_JOB_ITEM, jobItemEntity.getJobItem());
	}

	/**
	 * 查询岗位考核项目详细信息.
	 * 
	 * @param itemId 岗位考核项目ID
	 * @return 岗位考核项目详细信息
	 * @throws Exception 异常
	 */
	@SuppressWarnings("unchecked")
	public JobItemEntity searchJobItemById(Integer itemId) throws Exception
	{
		JobItemEntity jobItemEntity = new JobItemEntity();

		jobItemEntity.setJobItem((JobItemExEntity)this.getCommonDao().searchObj(
				Constants4Method.JOBEVA_SEARCH_JOB_ITEM_BY_ID, itemId));
		jobItemEntity.setJobRuleList(this.getCommonDao().searchList(
				Constants4Method.JOBEVA_SEARCH_JOB_RULE_BY_ITEM_ID,
				jobItemEntity.getJobItem().getItemId()));

		return jobItemEntity;
	}
}
